package danran.regular;

import java.util.*;

/**
 * @Classname FindSubstring
 * @Description TODO
 * @Date 2021/10/14 10:08
 * @Created by ASUS
 */
public class FindSubstring {
    public List<Integer> findSubstring(String s, String[] words) {
        if ("".equals(s) || words.length == 0) return new ArrayList<>();
        int length = words[0].length() * words.length;
        List<String> ws = Arrays.asList(words);
        Collections.sort(ws);
        List<String> tmp = new ArrayList<>();
        boolean f;
        List<Integer> ans = new ArrayList<>();
        for (int i = 0; i <= s.length() - length; i++) {
            f = true;
            String sub = s.substring(i, i + length);
            tmp.clear();
            for (int j = 0; j <= sub.length() - words[0].length(); j += words[0].length()) {
                tmp.add(sub.substring(j, j + words[0].length()));
            }
            Collections.sort(tmp);
            for (int j = 0; j < tmp.size(); j++) {
                if (!tmp.get(j).equals(ws.get(j))) {
                    f = false;
                    break;
                }
            }
            if (f) ans.add(i);
        }
        return ans;
    }

    public static void main(String[] args) {
        /*
        "wordgoodgoodgoodbestword"
["word","good","best","word"]


         */
        String[] words = new String[]{"word","good","best","word"};
        System.out.println(new FindSubstring().findSubstring("wordgoodgoodgoodbestword", words));
    }
}
